// https://www.misterma.com/archives/892/
//

const sqlite3 = require('sqlite3');
const path    = require('path');

const db_name = path.join(__dirname, 'data.db');

const db = new sqlite3.Database(db_name, err => {
    if(err != null)
        console.log(err);
});

// SQL语句
const sql = `
CREATE TABLE user(
    id INTEGER PRIMARY KEY,
    user_name VARCHAR(30) NOT NULL,
    age TINYINT(3) NOT NULL
)
`;

db.run(sql, err => {
    if(err)
        console.log(err);
    else
        console.log("create table successfully!");
});


const insert_sql = `
INSERT INTO user (id, user_name, age)
            VALUES(1, "nachr", 21)
`;

db.run(insert_sql, err => {
    if(err)
        console.log(err);
    else
        console.log("insert successfully!");
});

data_list = [
    data1={
        id: 2,
        user_name: "luwx",
        age: 20
    },
    data2={
        id: 3,
        user_name: "tx",
        age: 20
    }
];

const i_sql = `
INSERT INTO user (id, user_name, age)
            VALUES(?, ?, ?)
`;

for(i = 0; i < data_list.length; i++){
    db.run(i_sql, [data_list[i].id, data_list[i].user_name,
        data_list[i].age], err => {
            if(err)
                console.log(err);
        })
}




